package datasource;

import datasource.config.C3P0Config;
import homeworkjdbc.domain.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */
public class UserDao {
    public int pageSize=3;

    public List<User> findPage(int page) throws SQLException {
        //获得数据库连接
        Connection conn= C3P0Config.getConnection();
        List<User> list=new ArrayList<>();
        //编写sql语句
        String sql="select * from user order by id asc limit ?,? ";
        //预编译处理
        PreparedStatement ps=conn.prepareStatement(sql);
        //执行语句
        ps.setInt(1,(page-1)*pageSize);
        ps.setInt(2,pageSize);
        ResultSet set=ps.executeQuery();
        //处理结果
        while (set.next()){
            User user=new User();
            user.setId(set.getInt(1));
            user.setUsername(set.getString(2));
            user.setPassword(set.getString(3));
            user.setEmail(set.getString(4));
            user.setPhone(set.getInt(5));
            list.add(user);
        }
        return list;

        //由于数据库连接池的存在，不用手动断开
    }
}
